<?php
/**
 * Created by PhpStorm.
 * User: 段涛
 * Date: 2016/3/31
 * Time: 10:11
 */
namespace Home\Controller;

use Think\Controller;

class BillingVideoPlayController extends Controller
{

//    /*
//     * 清除数据，用于测试,后期删除 ，todo
//     */
//    public function clearData(){
//        $sql='DELETE FROM `mt_billing`';   //清空结算表
//        $result=M('')->query($sql);
//        $sql='UPDATE `mt_copyright_order` SET `video_play_is_clear`=0 '; //还原播放记录处理状态
//        $result=M('')->query($sql);
//        $where['billing_video_play_type']=array('GT',0);
//        $where['billing_year']=array('GT',0);
//        //还原年返利情况的中介数据
//        $list=M('agent')->field('id,billing_year,agent_time')->where($where)->select();
//        foreach ($list as $rs){
//            $rs['billing_year']=date('Y',$rs['agent_time']);
//            M('agent')->save($rs);
//        }
//        echo 'success';
//    }

/*    public function BillingAllData(){  //年结算强行操作
        $Billing = new \Common\Logic\BillingVideoPlayLogic();
        $Billing->yearBillingBalance(10,true);
    }*/

    /*
     * 计划任务
     */
    public function Scheduler ()
    {
        $Billing = new \Common\Logic\BillingVideoPlayLogic();
        $this->beginning(true); //效验结算表数据，补全所有结算表数据
        $this->balance(true); //效验结算表数据，按月自动补全结算表
        $Billing->clearVideoPlayData();  //平台计费系统数据更新
        $this->BillingBalance(true); // 对结算表进行进行结算
        //$Billing->yearBillingBalance(); //每年年初计算返利内容

        echo 'success';
    }

    /**
     * *效验结算表数据，自动补全结算表
     * 每天凌晨3点到6点
     * @param bool $isCheck 开启时间段执行，默认开启
     * FunctionName：balance
     * author: DuanTao
     */
    public function balance ($isCheck = false)
    {
        $Billing = new \Common\Logic\BillingVideoPlayLogic();
        $checkTimeStart = date('Y-m-d') . ' 03:0:0';
        $checkTimeEnd = date('Y-m-d') . ' 06:0:0';
        if (strtotime($checkTimeStart) < time() && strtotime($checkTimeEnd) > time()){
            $Billing->checkBalanceCinema();
            $Billing->checkBalanceAgent();
        }
        if ($isCheck !== false){
            $Billing->checkBalanceCinema();
            $Billing->checkBalanceAgent();
        }
    }

    /**
     * * 对结算表进行进行结算,默认最大处理10条数据
     * 每天凌晨0点到3点开始计算
     * @param bool $isCheck 开启时间段执行，默认开启
     * FunctionName：BillingBalance
     * author: DuanTao
     */
    public function BillingBalance ($isCheck = false)
    {
        //结算时间
        $Billing = new \Common\Logic\BillingVideoPlayLogic();
        $checkTimeStart = date('Y-m-d') . ' 00:0:0';
        $checkTimeEnd = date('Y-m-d') . ' 03:0:0';
        if (strtotime($checkTimeStart) < time() && strtotime($checkTimeEnd) > time()){
            $Billing->checkBillingAll(10);
        }
        if ($isCheck !== false){
            $Billing->checkBillingAll(10);
        }
    }

    /**
     *初始生成所有表缺失数据
     * FunctionName：beginning
     * 每天早上7点到8点
     */
    public function beginning ($isCheck = false)
    {
        M('')->query('DELETE FROM mt_billing WHERE billing_video_play_type is null');
        $Billing = new \Common\Logic\BillingVideoPlayLogic();
        $checkTimeStart = date('Y-m-d') . ' 07:0:0';
        $checkTimeEnd = date('Y-m-d') . ' 08:0:0';
        if (strtotime($checkTimeStart) < time() && strtotime($checkTimeEnd) > time()){
            $Billing->checkBalanceCinema(true);
            $Billing->checkBalanceAgent(true);
        }
        if ($isCheck !== false){
            $Billing->checkBalanceCinema(true);
            $Billing->checkBalanceAgent(true);
        }
    }

}